<template>
  <div class="container">
    <div class="app-container" style="margin-right: 20px">
      <el-button type="primary" @click="showDialog=true">添加角色</el-button>
      <el-table border :data="tableData">
        <el-table-column label="序号" type="index" align="center" />
        <el-table-column prop="name" label="角色" width="100">
          <template #default="{row}">
            <span v-if="!row.isEdit">{{ row.name }}</span>
            <!-- 绑定临时数据 临时数据随便你折腾  我有备份 真正的数据在row.name -->
            <el-input v-else v-model="row.temp.name" size="mini" />
          </template>
        </el-table-column>

        <!-- state是1 显示 启用 否则未启用 -->
        <!-- <el-table-column prop="state" label="是否启用" width="100" /> -->
        <el-table-column label="是否启用" width="100">
          <template #default="{row}">
            <span v-if="!row.isEdit">{{ row.state === 1 ? '启用' : '未启用' }}</span>
            <el-switch
              v-else
              v-model="row.state"
              active-color="#13ce66"
              inactive-color="#ff4949"
              :active-value="1"
              :inactive-value="0"
              size="mini"
            />
          </template>
        </el-table-column>

        <el-table-column prop="description" label="描述">
          <template #default="{row}">
            <span v-if="!row.isEdit">{{ row.description }}</span>
            <el-input v-else v-model="row.temp.description" size="mini" />
          </template>
        </el-table-column>

        <el-table-column label="操作">
          <!-- 通过默认插槽指定结构 -->
          <!--  -->
          <!-- 文字按钮 -->
          <template #default="{row}">
            <!-- {{ row }} -->
            <!-- obj是这个组件的作者在封装el-table-column组件时，传递给默认插槽的参数 -->
            <div v-if="!row.isEdit">
              <el-button type="text">分配权限</el-button>
              <el-button type="text" @click="row.isEdit=true">编辑</el-button>
              <el-popconfirm title="这是一段内容确定删除吗？" @onConfirm="confirmDel(row.id)">
                <el-button slot="reference" style="margin-left:10px" type="text">删除</el-button>
              </el-popconfirm>

            </div>
            <div v-else>
              <el-button type="primary" size="mini" @click="edit(row)">确定</el-button>
              <el-button size="mini" @click="cancelEdit(row)">取消</el-button>
            </div>
          </template>
        </el-table-column>
      </el-table>
      <div class="block">
        <el-pagination
          small
          :current-page="page"
          :page-sizes="[3,4,5,6]"
          :page-size="pagesize"
          layout="sizes, prev, pager, next, jumper, total"
          :total="total"
          @current-change="handleCurrentChange"
          @size-change="handleSizeChange"
        />
      </div>
      <!-- 新增角色对话框 -->
      <el-dialog title="新增角色" :visible.sync="showDialog" width="500px" @close="clear">
        <template #title>新增</template>

        <el-form ref="xx" :model="formObj" :rules="rules" label-width="100px" size="mini">
          <el-form-item label="角色名称" prop="name">
            <el-input v-model="formObj.name" />
          </el-form-item>
          <el-form-item label="启用">
            <!-- <el-input></el-input> -->
            <el-switch
              v-model="formObj.state"
              active-color="#13ce66"
              inactive-color="#ff4949"
              :active-value="1"
              :inactive-value="0"
            />
          </el-form-item>
          <el-form-item label="角色描述" prop="description">
            <el-input v-model="formObj.description" type="textarea" />
          </el-form-item>
        </el-form>

        <template #footer>
          <el-button size="mini" type="primary" @click="add">确定</el-button>
          <el-button size="mini" @click="clear">取消</el-button>
        </template>
      </el-dialog>
    </div>
  </div>
</template>
<script>
import { getRolesList, addRole, updateRole, deleteRole } from '@/api/role'
export default {

  name: 'Role',
  data() {
    return {
      tableData: [],
      total: 0,
      page: 1,
      pagesize: 5,
      showDialog: false,
      isEdit: false,
      formObj: {
        name: '',
        state: 1,
        description: '' },
      rules: {
        name: [{ required: true, message: '请输入角色名称', trigger: 'blur' }],
        description: [{ required: true, message: '请输入角色描述', trigger: 'blur' }]
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    async del(id) {
      await deleteRole(id)
      this.$message.success('删除成功')
      if (this.page !== 1 && this.tableData.length === 1) {
        this.page--
      }
      this.getList()
    },
    async edit(row) {
      await updateRole({
        ...row.temp,
        id: row.id
      })
      this.$message.success('编辑成功')
      this.getList()
    },
    cancelEdit(row) {
      row.temp.name = row.name
      row.temp.state = row.state
      row.temp.description = row.description
      row.isEdit = false
    },
    clear() {
      this.showDialog = false
      this.$refs.xx.resetFields()
      this.formObj = {
        name: '',
        state: 1,
        description: ''
      }
    },
    add() {
      this.$refs.xx.validate(async(isOk) => {
        if (isOk) {
          await addRole(this.formObj)
          this.showDialog = false
          this.$message.success('添加成功')

          this.clear()
          this.getList()
        }
      })
    },
    async getList() {
      const { rows, total } = await getRolesList(this.page, this.pagesize)
      this.tableData = rows
      this.tableData.forEach((item) => {
        this.$set(item, 'isEdit', false)
        this.$set(item, 'temp', {
          name: item.name,
          state: item.state,
          description: item.description
        })
      })
      this.total = total
    },
    handleCurrentChange(val) {
      this.page = val
      this.getList()
    },
    handleSizeChange(val) {
      this.pagesize = val
      this.page = 1
      this.getList()
    }
  }
}
</script>
